package com.wanlian.service.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.QuestionDao;
import com.wanlian.dao.QuestionAnswerDao;
import com.wanlian.dao.QuestionTypeDao;
import com.wanlian.dao.UserDao;
import com.wanlian.entity.Question;
import com.wanlian.entity.QuestionAnswer;
import com.wanlian.entity.QuestionType;
import com.wanlian.entity.User;
import com.wanlian.model.ResultModel;
import com.wanlian.service.QuestionServices;
import com.wanlian.util.DateUtil;
import com.wanlian.vo.Page;
import com.wanlian.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class QuestionServicesImpl implements QuestionServices {

    @Autowired
    private UserDao userDao;

    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private QuestionTypeDao questionTypeDao;

    @Autowired
    private QuestionAnswerDao questionAnswerDao;


    @Override
    public ResultModel queryMessage(Page page, Map<String, Object> conditions) {
        return questionDao.queryMessage(page, conditions);
    }

    @Override
    public ResultModel doAdd(Question question) {
        ResultModel resultModel = new ResultModel();
        question.setLastUpdateTime(DateUtil.currentTimestamp());
        String content=question.getContent();
        if(content!=null){
            int size =content.length();
            if(size>300){
                resultModel.setBresult(false);
                resultModel.setMessage("300");
                return  resultModel;
            }
        }
        questionDao.add(question);
        resultModel.setBresult(true);
        return resultModel;
    }


    @Override
    public ResultModel doAdd(QuestionType questionType) {
        ResultModel resultModel = new ResultModel();
        QuestionType questionTypeDb = questionTypeDao.queryById(questionType.getId());
        if(questionType.getModelName()==null||questionType.getModelName()==""){
            resultModel.setBresult(false);
            resultModel.setMessage("问题类型名称不能为空");
            return resultModel;
        }
        if(questionType.getModelDesc()==null||questionType.getModelDesc()==""){
            resultModel.setBresult(false);
            resultModel.setMessage("问题内容不能为空");
            return resultModel;
        }

        if (questionTypeDb == null) {
            questionType.setLastUpdateTime(DateUtil.currentTimestamp());
            questionTypeDao.add(questionType);
            resultModel.setBresult(true);
        } else {
            resultModel.setBresult(false);
            resultModel.setMessage("该问题类型已存在");
        }
        return resultModel;
    }

    @Override
    public ResultModel doUpd(QuestionType questionType) {
        ResultModel resultModel = new ResultModel();
        questionTypeDao.update(questionType);
        resultModel.setBresult(true);
        resultModel.setMessage("修改成功");
        return resultModel;
    }

    @Override
    public ResultModel doDel(QuestionType questionType) {
        ResultModel resultModel = new ResultModel();
        long count =questionDao.queryQuestionCountByTypeId(questionType.getId());
        if(count>0){
            resultModel.setBresult(true);
            resultModel.setMessage("该问题类型下还有用户问题，无法删除");
        }else{
            questionTypeDao.delete(questionType.getId());
            resultModel.setBresult(true);
            resultModel.setMessage("删除成功");
        }
        return resultModel;
    }

    @Override
    public ResultModel queryTypeList(Page page, Map<String, Object> conditions) {
        return questionTypeDao.queryMessage(page, conditions);
    }

    @Override
    public ResultModel queryQuestionAnswer(Page page, Map<String, Object> conditions) {
        return questionAnswerDao.queryQuestionAnswer(page, conditions);
    }

    @Override
    public ResultModel doAnswerAdd(QuestionAnswer questionAnswer) {
        ResultModel resultModel = new ResultModel();
        Long count = questionAnswerDao.queryCountById(questionAnswer.getQuestionId());
        if (count == 0) {
            questionAnswer.setLastUpdateTime(DateUtil.currentTimestamp());
            questionAnswerDao.add(questionAnswer);
            resultModel.setMessage("回复成功");
        } else {
            resultModel.setMessage("无法重复回复");
        }

        return resultModel;
    }

    @Override
    public ResultModel queryTypeById(int id) {
        return questionTypeDao.queryQuestionTypeById(id);
    }


    @Override
    public ResultModel queryAllUserQuestion(PageCondition pageCondition) {
        ResultModel resultModel =questionDao.queryQueAnsByParams(pageCondition);
        return resultModel;
    }
}
